package edu.bit.water.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import edu.bit.water.entity.Chart;
import edu.bit.water.service.WbalChartService;

@Controller
@RequestMapping("/wbalchart")
public class WbalChartController {

	@Autowired
	WbalChartService wbalChartService;
	
	/**
     * 增加流程图相关数据的方法
     *
     * @param jsonObject 流程图实体类
     * @return 返回操作信息
     */
	@ResponseBody
    @RequestMapping("/insert")
    public Map<String, String> insert(@RequestBody JSONObject jsonObject) {
        Chart chart = new Chart();
        chart.setChartName(jsonObject.getString("chartName"));
        chart.setData(jsonObject.getString("data"));
        chart.setBusinessId(Integer.parseInt(jsonObject.getString("businessId")));
        return wbalChartService.addChart(chart);
    }

    @ResponseBody
    @RequestMapping(value = "/select_by_businessid", method = RequestMethod.GET)
    public Map<String, Object> selectAll(String company_id) {
        List<Chart> ls = wbalChartService.selectChartByBusiness(Integer.parseInt(company_id));
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("rows", ls);
        return modelMap;
    }

    /**
     * 根据chartName查询流程图信息的方法
     *
     * @return 成功返回Chart集合, 失败返回NULL
     */
    @ResponseBody
    @RequestMapping("/selectOne")
    public Map<String, Object> selectOne(int id) {
        Chart chart = wbalChartService.selectOne(id);
        Map<String, Object> modelMap = new HashMap<>();
        modelMap.put("result", chart);
        return modelMap;
    }

    /**
     * 修改某个流程图数据
     *
     * @param jsonObject 将要被修改的流程图数据
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map<String, String> update(@RequestBody JSONObject jsonObject) {
        Chart chart = new Chart();
        chart.setId(jsonObject.getIntValue("id"));
        chart.setChartName(jsonObject.getString("chartName"));
        chart.setData(jsonObject.getString("data"));
        return wbalChartService.updateChart(chart);
    }

    /**
     * 删除流程图的方法
     *
     * @param idList 将要被删除id的list集合
     * @return 返回操作信息
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idList) {
        int n = wbalChartService.deleteChart(idList);
        Map<String, String> map = new HashMap<>();
        if (n == 1)
            map.put("status", "success");
        else
            map.put("status", "failed");
        return map;
    }
}
